#include<stdio.h>
#include<stdbool.h>
 struct ListNode {
    int val;
    struct ListNode* next;
   
};

typedef struct ListNode ListNode;
bool hasCycle(struct ListNode* head)
{
    ListNode* slow = head;
    ListNode* fast = head;
    while (fast && fast->next)
    {
        fast = fast->next->next;
        slow = slow->next;
        if (fast == slow)
            return true;
    }
    return false;
}
struct ListNode 
{
    int val;
    struct ListNode* next;
    
};


typedef struct ListNode ListNode;
struct ListNode* detectCycle(struct ListNode* head)
{
    ListNode* fast = head;
    ListNode* slow = head;
    while (fast && fast->next)
    {
        fast = fast->next->next;
        slow = slow->next;
        if (slow == fast)
        {
            ListNode* pcr = head;
            while (pcr != slow)
            {
                pcr = pcr->next;
                slow = slow->next;
            }
            return pcr;
        }
    }
    return NULL;
}